package com.xsy.android.compose.wandroid.ui.page.artice

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Share
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
import com.xsy.android.compose.wandroid.R
import com.xsy.android.compose.wandroid.logic.model.ArticleModel
import com.xsy.android.compose.wandroid.logic.utils.getHtmlText
import com.xsy.android.compose.wandroid.ui.view.TopAppBar
import com.xsy.android.compose.wandroid.ui.view.rememberWebViewWithLifecycle

/**
 *@author: xiaosy
 *时间: 2024/7/4
 *@Description: java类作用描述
 *@Version: 1.0
 **/
@SuppressLint("SetJavaScriptEnabled")
@Composable
fun ArticlePage(
    modifier: Modifier = Modifier,
    article: ArticleModel?,
    onBack: () -> Unit
) {
    val context = LocalContext.current
    val webView = rememberWebViewWithLifecycle()
    Scaffold { innerPadding ->
        Column(
            modifier = modifier
                .padding(innerPadding)
        ) {
            TopAppBar(title = getHtmlText(article?.title ?: "文章详情"), click = {
                if (webView.canGoBack()) {
                    webView.goBack()
                } else {
                    onBack.invoke()
                }
            }, showRight = true, rightImg = Icons.Filled.Share, rightClick = {
                sharePost(article?.title, article?.link, context)
            })
            AndroidView(
                factory = { webView }, modifier = modifier
                    .fillMaxSize()
            ) { view ->
                view.webViewClient = object : WebViewClient() {
                    override fun shouldOverrideUrlLoading(
                        view: WebView?,
                        request: WebResourceRequest?
                    ): Boolean {
                        return try {
                            val scheme = request?.url?.scheme
                            if (!arrayOf("http", "https").contains(scheme)) {
                                val intent = Intent(Intent.ACTION_VIEW, request?.url)
                                webView.context.startActivity(intent)
                            }
                            true
                        } catch (e: Exception) {
                            super.shouldOverrideUrlLoading(view, request)
                        }
                    }
                }
                val settings: WebSettings = view.settings
                settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
                settings.javaScriptEnabled = true
                settings.blockNetworkImage = false
                view.loadUrl(article?.link ?: "")
            }
        }
    }
}

private fun sharePost(title: String?, post: String?, context: Context) {
    if (title == null || post == null) {
        return
    }
    val intent = Intent(Intent.ACTION_SEND).apply {
        type = "text/plain"
        putExtra(Intent.EXTRA_TITLE, title)
        putExtra(Intent.EXTRA_TEXT, post)
    }
    context.startActivity(Intent.createChooser(intent, context.getString(R.string.share_article)))
}

@Preview
@Composable
private fun PreviewArticlePage() {
    ArticlePage(
        article = ArticleModel(
            title = "标题",
            superChapterName = "分类",
            author = "作者"
        )
    ) {}
}